package com.fund.user.controller;

import com.fund.user.base.BaseController;
import com.fund.user.config.OssTemplate;
import com.fund.user.service.TUserService;
import com.fund.user.vo.req.UserRegistVo;
import com.fund.user.vo.resp.UserRespVo;
import com.fund.utils.AppResponse;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.text.ParseException;
import java.util.UUID;


/**
 * @author Tght
 * @RefreshScope 配置自动刷新
 */
@Slf4j
@RefreshScope
@RestController
@RequestMapping("/start")
public class UserLoginController extends BaseController {



    @Autowired
    StringRedisTemplate stringRedisTemplate;

    @Autowired
    TUserService tUserService;


    @Autowired
    OssTemplate ossTemplate;

    /**
     * 文件上传表单提交要求：
     * 1、method="'post"
     * 2、enctype="multipart/from-data"
     * 3、type="file" name="uploadfile"
     * <p>
     * SpringMVC框架集成commons-fileupload和commons-io组件，完成文件上传操作
     * SpringMVC提供了文件上传解析器。
     * Controller处理文件上传时，通过MultipartFile接收文件。
     *
     * @return
     */
    @CrossOrigin
    @PostMapping("/imageUrl")
    public AppResponse<Object> image(MultipartFile file) {
        try {
            String filename = file.getOriginalFilename();
            filename = UUID.randomUUID().toString().replaceAll("-", "") + "-" + filename;
            String filePath = ossTemplate.upload(filename, file.getInputStream());
            return AppResponse.ok(filePath);
        } catch (Exception e) {
            return AppResponse.fail(null);
        }
    }


    /**
     * 账号密码-登录
     */
    @GetMapping("/login")
    public AppResponse<UserRespVo> login(@RequestParam("account") String account, @RequestParam("password") String password) {
        log.debug("登录表单数据account-{} \n 登录表单数据password-{}登录表单数据password-{}", account,password);
        return tUserService.getUserByLogin(account, password);
    }

    /**
     * 手机验证码-登录
     */
    @GetMapping("/loginPhone")
    public AppResponse<Object> loginPhone(@RequestParam("phone") String phone, @RequestParam("code") String code) {
        return tUserService.loginPhone(phone, code);
    }


    /**
     * 邮箱验证码-登录
     */
    @GetMapping("/loginEmail")
    public AppResponse<Object> loginEmail(@RequestParam("email") String email, @RequestParam("code") String code) {
        return tUserService.loginEmail(email, code);
    }


    /**
     * 用户注册
     * param 昵称、账户（邮箱or手机号）、密码、验证码
     * @PostMapping 只能与@RequestBody 一起用
     */
    @PostMapping("/reg")
    public AppResponse<Object> register(@RequestBody UserRegistVo vo) throws ParseException {
        return tUserService.register(vo);
    }

    /**
     * 发送短信验证码
     */
    @GetMapping("/sendSms")
    public AppResponse<Object> sendSms(@RequestParam("account") String account) {
        return tUserService.sendSms(account);
    }

    /**
     * 注册时发送验证码
     *
     * @param account
     * @return
     */

    @GetMapping("/sendCode")
    public AppResponse<Object> sendCode(@RequestParam("account") String account) {
        return tUserService.sendCode(account);
    }


    /**
     * 发送邮件注册验证码
     */
    @GetMapping("/sendEmailReg")
    public AppResponse<Object> sendEmailReg(@RequestParam("email") String email) {
        return tUserService.sendEmailReg(email);
    }

    /**
     * 发送邮件登录验证码
     *
     * @param email
     * @return
     */
    @GetMapping("/sendEmailLog")
    public AppResponse<Object> sendEmailLog(@RequestParam("email") String email) {
        return tUserService.sendEmailLog(email);
    }
}
